'''
log.py
Nicholas Swartzendruber

This is a test for the static log class.
Should act like System.out in Java, but appends to a file. 
'''
from time import strftime
import os
  
class Log:
  '''
  The print method is used to append to the end
  of a log file. Uses:
  1) By leaving the optional file parameter blank,
  a default log file will be written to. This log
  file can be found in "<current-dir>/log/<date>.txt".
  A time stamp will be produced on each println. 
  
  2)By passing a file or directory path (that ends
  in .txt), the text will be appended to the end of
  that file. A time stamp will not be produced 
  automatically.
  '''
  @staticmethod
  def println(text, file = None):
    timestamp = ''
    if file == None:
      file = 'logs/' + strftime("%Y_%m_%d") + '.txt'
      timestamp = strftime("%H:%M:%S") + " >> "
    
    last_index = len(file) - 1 - file[::-1].find('/')
    path = file[:last_index]
    if '/' in file and not os.path.exists(path):
      os.mkdir(path)
    with open(file, 'a+') as f:
          f.write(str(timestamp + text + '\n'));
  
          
'''
Test the log class
'''
if __name__ == '__main__':
  Log.println("Hello, world.")
  Log.println("Hello, world.", "test.txt")
  Log.println("Tweet!", "storage/tweets.txt")
  print 'finished'